os.loadAPI("/System/API/sysbar")
local Args = {...}
local run = true
local xSize, ySize = term.getSize()
local cursor = 0
local path = Args[1]
local text = ''
local line = 0
local step = 2
local run = true
local screen = window.create(term.current(),1,3,xSize,ySize-3)

local clear = function(color)
	term.setBackgroundColor(color)
	term.clear()
	term.setCursorPos(1,1)
end

local usualText = function(string,xPos,yPos)
	term.setCursorPos(xPos,yPos)
	term.write(string)
end

local sysBar = function()
	sysbar.header(path,mainTextCol,mainColor)
	local i = 1
	local curs = -cursor
	while curs > 9 do
	 curs = math.floor(curs/10)
	 i = i + 1
	end
	usualText('Ln: '..-cursor+1,xSize-i-3,1)
	i = 1
	curs = line
	while curs > 9 do
	 curs = math.floor(curs/10)
	 i = i + 1
	end
	usualText('of '..line-1,xSize-i-2,2)
	sysbar.draw(colors.white,colors.black,noty,true,false)
end

local redraw = function()
	term.redirect(screen)
	clear(colors.white)
	term.setTextColor(colors.black)
	line = 0
	local file = fs.open(path,'r')
	text = file.readLine()
	while text~=nil do
		usualText(text,2,line+1+cursor)
		local i = 1
		while text ~= nil and #text > xSize*i do
			line = line + 1
			usualText(text,2-xSize*i+i,line+1+cursor)
			usualText(string.char(16),1,line+1+cursor)
			i = i + 1
		end
		line = line + 1
		text = file.readLine()
	end
	file.close()
	term.redirect(term.native())
	sysBar()
end

local running = function()
 while run == true do
  event, side, x, y = os.pullEvent()
  if event == "mouse_scroll" then
   if line>ySize-3 then
    if side == 1 then
     if cursor>-line+ySize-2 then
      cursor = cursor-2
     else
      cursor = -line+ySize-2
     end 
    elseif side == -1 then
     if cursor<0 then 
      cursor = cursor+2
     else
      cursor = 0
     end
    end
    redraw()
   end
  elseif event == "mouse_click" then
   if sysbar.back(x,y) == true then
    clear(colors.black)
	run = false
   elseif sysbar.home(x,y) == true then
    clear(colors.black)
	home = true
    run = false
	break
   end
  end
  term.redirect(term.native())
 end
end

redraw()
running()